package com.socks.okhttp.plus.builder;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.IdentityHashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.socks.okhttp.plus.OkHttpProxy;
import com.socks.okhttp.plus.callback.OkCallback;
import com.thoughtworks.xstream.XStream;

import android.text.TextUtils;
import android.util.Log;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 */
public class DataPostRequestBuilder<T> extends RequestBuilder<T> {

    private Map<String, String> headers;

    public static final MediaType mediaType= MediaType.parse("text/x-markdown; charset=GB2312");

    private String dataStr ;

    public DataPostRequestBuilder<T> url(String url) {
        this.url = url;
        return this;
    }


    public DataPostRequestBuilder<T> setDataStr(String dataStr){
        this.dataStr = dataStr;
        return this;
    }

    public DataPostRequestBuilder<T> setDataJson(Object o){
        this.dataStr = new Gson().toJson(o);
        return this;
    }

    public DataPostRequestBuilder<T> setDataXml(Object o){
        XStream xStream = new XStream();
        xStream.alias(o.getClass().getName(),o.getClass());
//        this.dataStr = xStream.toXML(o);
        
        @SuppressWarnings("deprecation")
		String sendXml = URLEncoder.encode(xStream.toXML(o)); //转码
        this.dataStr = sendXml;

//        this.dataStr ="<?xml version='1.0' encoding='utf-8'?>                     "
//        		+"<com.estar.app.appsrvycabx.domain.DataVO>                  "
//        		+"  <action>QAndroidSubmitVideoSrvy</action>                 "
//        		+"  <list>                                                   "
//        		+"    <com.estar.app.appsrvyspds.domain.SubmitVideoSrvyVO>   "
//        		+"      <carNo>京HJ9292</carNo>                              "
//        		+"      <caseType>1001</caseType>                            "
//        		+"      <claimTaskId>1469845708486</claimTaskId>             "
//        		+"      <claimTaskType>3</claimTaskType>                     "
//        		+"      <clientType/>                                        "
//        		+"      <companyNme/>                                        "
//        		+"      <latitude/>                                          "
//        		+"      <longitude/>                                         "
//        		+"      <mbOperateFlag>1</mbOperateFlag>                     "
//        		+"      <plyDptCde>32000002</plyDptCde>                      "
//        		+"      <plyDptNme>董事会</plyDptNme>                        "
//        		+"      <pressLevel/>                                        "
//        		+"      <setLossMark/>                                       "
//        		+"      <sryCde>3200000102</sryCde>                          "
//        		+"      <sryNme>张秋</sryNme>                                "
//        		+"      <taskNo>705202016320600000068</taskNo>               "
//        		+"    </com.estar.app.appsrvyspds.domain.SubmitVideoSrvyVO>  "
//        		+"  </list>                                                  "
//        		+"  <method>pdaSubmitVideoSrvy</method>                      "
//        		+"</com.estar.app.appsrvycabx.domain.DataVO>                 ";
        
       
        Log.i("11","111"+dataStr);
        return this;
    }
    public DataPostRequestBuilder<T> headers(Map<String, String> headers) {
        this.headers = headers;
        return this;
    }

    public DataPostRequestBuilder<T> addHeader(String key, String values) {
        if (headers == null) {
            headers = new IdentityHashMap<String, String>();
        }
        headers.put(key, values);
        return this;
    }

    public DataPostRequestBuilder<T> tag(Object tag) {
        this.tag = tag;
        return this;
    }


    @Override
    public Call enqueue(Callback callback) {

        if (TextUtils.isEmpty(url)) {
            throw new IllegalArgumentException("url can not be null !");
        }

        Request.Builder builder = new Request.Builder().url(url);
        if (tag != null) {
            builder.tag(tag);
        }
        RequestBody body = RequestBody.create(mediaType,dataStr);
        appendHeaders(builder, headers);
        builder.post(body);

        Request request = builder.build();

        if (callback instanceof OkCallback) {
            ((OkCallback<?>) callback).onStart();
        }
        Call call = OkHttpProxy.getInstance().newCall(request);
        call.enqueue(callback);
        return call;
    }

    @Override
    public Response execute() throws IOException {
        if (TextUtils.isEmpty(url)) {
            throw new IllegalArgumentException("url can not be null !");
        }

        Request.Builder builder = new Request.Builder().url(url);
        if (tag != null) {
            builder.tag(tag);
        }
        RequestBody body = RequestBody.create(mediaType,dataStr);
        appendHeaders(builder, headers);
        builder.post(body);
        Request request = builder.build();

        Call call = OkHttpProxy.getInstance().newCall(request);
        return call.execute();
    }
}
